/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Input.java
 *
 * Created on Sep 6, 2010, 7:19:42 PM
 */

package ssdhddsimulator;


import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
 *
 * @author Kartikeya Dubey
 */
public class Input extends javax.swing.JFrame{

    /** Creates new form Input */
    public Input() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChoose = new javax.swing.JButton();
        filePath = new java.awt.TextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        fileChoose.setText("Choose File");
        fileChoose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileChooseMouseClicked(evt);
            }
        });
        fileChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileChooseActionPerformed(evt);
            }
        });

        filePath.setText("/Users/ammarhusain/Desktop/AI_Research/Data/");

        jLabel1.setText("SSD");

        jLabel2.setText(Double.toString(SSDtime));
        jLabel2.setName("SSDTimeLabel1"); // NOI18N

        jLabel3.setText("HDD");

        jLabel4.setText("Time (s)");

        jLabel5.setText("Power (mWs)");

        jLabel6.setText("Time (s)");

        jLabel7.setText("Power (mWs)");

        jLabel8.setText(Double.toString(HDDtime));

        jLabel9.setText("Time (s)");

        jLabel10.setText("Power (mWs)");

        jLabel11.setText("Hybrid Drive");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(filePath, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fileChoose)
                        .addGap(64, 64, 64))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(353, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(359, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addContainerGap(304, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addGap(116, 116, 116))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                                .addComponent(jLabel5)))
                        .addGap(120, 120, 120))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
                                .addComponent(jLabel7)))
                        .addGap(119, 119, 119))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(filePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fileChoose))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(5, 5, 5)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addGap(3, 3, 3)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {fileChoose, filePath});

        jLabel2.getAccessibleContext().setAccessibleName("SSDTimeLabel");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fileChooseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileChooseMouseClicked
        //Open a file chooser that allows the user to select a file

    }//GEN-LAST:event_fileChooseMouseClicked

    private void fileChooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooseActionPerformed
        //Read in file with specified path
        String path = filePath.getText();

        File dir = new File(path);
        StringBuffer FName = new StringBuffer(path);

        String[] children = dir.list();
        if (children == null) {
        // Either dir does not exist or is not a directory
            //System.out.println("No file");

        }
        else {
             for (int j=0; j<children.length; j++) {
                // Get filename of file or directory
                 String DirFile = children[j];
                 //System.out.println(DirFile);
                 String tmp = path.concat(DirFile);
                 readFile(tmp);
                 }
            }
        
    }//GEN-LAST:event_fileChooseActionPerformed

    void readFile(String FName)
    {
        System.out.println(FName);
        try
        {
            FileInputStream fs = new FileInputStream(FName.toString());
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fs);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //SSDtime = 0;
            //Read File Line By Line



            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              parseFileOperation(strLine);

            }
            //Close the input stream
            in.close();
        }
        catch(Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }


        jLabel2.setText(Double.toString(SSDtime));
        jLabel8.setText(Double.toString(HDDtime));
    }

    public double SSDtime = 0;
    public double HDDtime = 0;
    public Map fileLog = new HashMap();
    public String currFile;



    //Parse the line from the file and call appropriate functions
    //in SSD and HDD
    void parseFileOperation(String fileData)
    {
        int semiColonCount = 0;


        int i=0;

        StringBuffer offset = new StringBuffer();
        StringBuffer UIDconst = new StringBuffer();
        StringBuffer UID = new StringBuffer();
        StringBuffer PIDconst = new StringBuffer();
        StringBuffer PID = new StringBuffer();
        StringBuffer processName = new StringBuffer();
        StringBuffer ABstamp = new StringBuffer();
        StringBuffer timeStamp = new StringBuffer();
        StringBuffer sysCall = new StringBuffer();


            while(i <= fileData.length() - 1)
            {

                if(fileData.charAt(i) == ' ')
                {
                    if(semiColonCount<=8){
                    semiColonCount++;
                }

                       // System.out.println("hello");
                }
                else
                {
System.out.println(semiColonCount);
                    switch(semiColonCount){
                        case 0: offset.append(fileData.charAt(i)); break;
                        case 1: UIDconst.append(fileData.charAt(i)); break;
                        case 2: UID.append(fileData.charAt(i)); break;
                        case 3: PIDconst.append(fileData.charAt(i)); break;
                        case 4: PID.append(fileData.charAt(i)); break;
                        case 5: processName.append(fileData.charAt(i)); break;
                        case 6: ABstamp.append(fileData.charAt(i)); break;
                        case 7: timeStamp.append(fileData.charAt(i)); break;
                        case 8: sysCall.append(fileData.charAt(i)); break;
                        //Additonal variables can be added to store return values
                        //case 9: break;
                        //case 10: break;
                        default:  break;
                    }
                 }
                i++;
        }

        System.out.println(sysCall);
        String fileName = parseSysCall(sysCall);

        System.out.println(fileName);


        //double fileSize = (Double.parseDouble(Size512B.toString())) * 512 * 9.53674316 * Math.pow(10,-7);
        ////Passing in 0 as the offset because actual offset is unknown
        //at the moment
        //SSDtime += SSD.SSDCalculator(IOType.toString(), fileSize, 0);
        //System.out.println(SSDtime);
        //HDDtime += HDD.HDDCalculator(IOType.toString(), fileSize, 0);

        //Update GUI
        //jLabel2.setText(Double.toString(SSDtime));
        //jLabel8.setText(Double.toString(HDDtime));
    }

    String parseSysCall(StringBuffer call){

        StringBuffer a = new StringBuffer();
        StringBuffer b = new StringBuffer();
        StringBuffer c = new StringBuffer();
        StringBuffer d = new StringBuffer();
        StringBuffer e = new StringBuffer();
        int i = 0;
        int seperator = 0;

        while(i <= call.length() - 1)
            {

                if((call.charAt(i) == '(') | (call.charAt(i) == ','))
                {
                    seperator++;
                }
                else{
                    if(call.charAt(i) != ' '){ // in order to srop any leading spaces
                    switch(seperator){
                        case 0: a.append(call.charAt(i)); break;
                        case 1: b.append(call.charAt(i)); break;
                        case 2: c.append(call.charAt(i)); break;
                        case 3: d.append(call.charAt(i)); break;
                        case 4: e.append(call.charAt(i)); break;
                        }

                    }
                }
        }

        //adding the system call to the corresponding object

        String currSysCall = a.toString();

        //Create a system call object to associate with the file
        sysCallObj tempObj = new sysCallObj();

        if(currSysCall == "open"){

        currFile = b.toString();


        sysCallObj.sysCall = a.toString();

        //check if the file exists in the log
            if(fileLog.containsKey(currFile)){
                //The file exists in the log
                //Therefore add the system call to existing list
                fileObj tempList = (fileObj) fileLog.get(currFile);
                tempList.sysLog.add(tempObj);
            }
            else{
                //add the first system call of the log
                fileObj newFile = new fileObj();
                newFile.sysLog.add(tempObj);

            }
        }
        else{ //The file being operated is same as before
              //check if the file exists in the log
              if(fileLog.containsKey(currFile)){
                //The file exists in the log
                //Therefore add the system call to existing list
                fileObj tempList = (fileObj) fileLog.get(currFile);
                tempList.sysLog.add(tempObj);
                }

              //if the file has not been opened it is ignored temporarily
        }

        //Add in the file size calculation mechanism
      
    return currFile;
}



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Input().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton fileChoose;
    private java.awt.TextField filePath;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    // End of variables declaration//GEN-END:variables

    

}
